package joodle.domain;

import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;

/**
 * One of the basic shapes included in joodle out of box. It is a subclass of 
 * AbstractShape and inherits most of its methods. The draw, hasEnoughPoints, isPointOn 
 * and asXML methods will be overridden in these basic shapes classes to give the proper 
 * functionality to each of the basic shapes.
 */

public class Text extends AbstractShape {

	/**
	 * Sets the attributes to the default values for the text object
	 */
	public Text() {
		//setAttributeValue(Attribute.COLOR, Color.lightGray);
		setAttributeValue(Attribute.FILL_COLOR, DEFAULT_COLOR);	// fill the text
		setAttributeValue(Attribute.FONT, new Font("Verdana", Font.PLAIN, 46));
		setAttributeValue(Attribute.TEXT, "");
		attributes.remove(Attribute.COLOR);
	}
	
	protected Text(final Text t) {
		super(t);
	}
	
	/**
	 * Returns an XML representation of the text on a string format
	 * @see Shape#asXML()
	 * @return a string representation of the text
	 */
	@Override
	public String asXML() {
		StringBuilder sb = new StringBuilder();

		Font f = (Font) getAttributeValue(Attribute.FONT);
		
		sb.append(String.format("<text x=\"0\" y=\"0\" font-family=\"%s\" font-size=\"%d\" ",
				/*p1.getX(), p1.getY(), */f.getFamily(), f.getSize()));
		
		sb.append(attributesAsXML());
		sb.append(transformAsXML());
		
		sb.append(" >");

		String text = (String) getAttributeValue(Attribute.TEXT);
		sb.append(text);
		sb.append("</text>\n");
		
		return sb.toString();
	}

	/**
	 * Creates a clone of the Text shape
	 * @see Shape#cloneShape()
	 * @return a new Text shape
	 */
	@Override
	public Shape cloneShape() {
		return new Text(this);
	}

	/**
	 * Adds a point to the text and updates the text
	 * @see Shape#addPoint(Point)
	 */
	@Override
	public void addPoint(Point p) {
		if(points.isEmpty())
			currentTransform = AffineTransform.getTranslateInstance(p.getX(), p.getY());
		super.addPoint(p);
	}
	
	/**
	 * Checks if there are enough points to create a Point object
	 * @see Shape#hasEnoughPoints()
	 * @return a boolean value depends on the enough points condition
	 */
	@Override
	public boolean hasEnoughPoints() {
		return points.size() > 1;
	}

	/**
	 * Changes the value of the attribute with respect to the new value
	 * @param a is the attribute to be changed
	 * @param v is the new value
	 * @see Shape#setAttributeValue(joodle.domain.Shape.Attribute, Object)
	 */
	@Override
	public void setAttributeValue(Attribute a, Object v) {
		attributes.put(a, v);
		if(a == Attribute.TEXT || a == Attribute.FONT)
			remakeAwtShape();
	}

	/**
	 * Creates an AWT representation of the text
	 * @see AbstractShape#remakeAwtShape()
	 */
	@Override
	protected void remakeAwtShape() {
		String text = (String) getAttributeValue(Attribute.TEXT);
		if(text == null || text.isEmpty())
			return;

		Font font = (Font) getAttributeValue(Attribute.FONT);
		TextLayout tl = new TextLayout(text, font,
				new FontRenderContext(null, true, true));
		path = new Path2D.Float(tl.getOutline(null));
	}
}
